#include "AiTitleBar.h"
#include <QHBoxLayout>
#include <QSpacerItem>
#include <qt_windows.h>
#include <string>

AiTitleBar::AiTitleBar(QWidget *parent) : QWidget(parent)
{
    initUI();
}

// 析构
AiTitleBar::~AiTitleBar(){

}

void AiTitleBar::initUI(){
    // 禁止父窗口颜色影响子窗口颜色
    this->setAttribute(Qt::WA_StyledBackground);
    // 设置固定高度
    this->setFixedHeight(32 + 10);
    // 设置背景颜色
    this->setStyleSheet("background-color: rgb(222, 219, 219);");

    m_configBtn = new QPushButton(this);
    m_configBtn->setFixedSize(32, 32);
    m_configBtn->setStyleSheet("QPushButton{background-image:url(:/resource/set.svg);border:none;}"
                             "QPushButton:hover{"
                             "background-image:url(:/resource/set_hover.svg);border:none;}");
    
    m_pMinBtn = new QPushButton(this);
    m_pMinBtn->setFixedSize(32, 32);
    m_pMinBtn->setStyleSheet("QPushButton{background-image:url(:/resource/min.svg);border:none;}"
                             "QPushButton:hover{"
                             "background-image:url(:/resource/min_hover.svg);border:none;}");

    m_pCloseBtn = new QPushButton(this);
    m_pCloseBtn->setFixedSize(32, 32);
    m_pCloseBtn->setStyleSheet("QPushButton{background-image:url(:/resource/close.svg);border:none;}"
                               "QPushButton:hover{"
                               "background-image:url(:/resource/close_hover.svg);border:none;}");
    QHBoxLayout *layout = new QHBoxLayout(this);
    // 空隙
    // 添加间隙
    QSpacerItem *pItem1 = new QSpacerItem(20, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
    layout->addSpacerItem(pItem1);
    layout->addWidget(m_configBtn);
    // 弹簧
    layout->addStretch();
    QSpacerItem *pItem11 = new QSpacerItem(20, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
    layout->addSpacerItem(pItem11);
    layout->addWidget(m_pMinBtn);
    QSpacerItem *pItem2 = new QSpacerItem(20, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
    layout->addSpacerItem(pItem2);
    layout->addWidget(m_pCloseBtn);
    QSpacerItem *pItem3 = new QSpacerItem(20, 20, QSizePolicy::Fixed, QSizePolicy::Fixed);
    layout->addSpacerItem(pItem3);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    // 链接信号槽
    connect(m_configBtn, &QPushButton::clicked, this, &AiTitleBar::onBtnClicked);
    connect(m_pMinBtn, &QPushButton::clicked, this, &AiTitleBar::onBtnClicked);
    connect(m_pCloseBtn, &QPushButton::clicked, this, &AiTitleBar::onBtnClicked);

}


void AiTitleBar::onBtnClicked(){
    // 拿到发送事件的对象
    QPushButton *pBtn = qobject_cast<QPushButton *>(sender());
    // 拿到父窗口的指针
    QWidget *pWindow = this->window();
    // 判断是哪个按钮
    if (pBtn == m_configBtn)
    {
        // 配置
        sigConfigAi();
    }
    if (pBtn == m_pMinBtn)
    {
        // 最小化
        pWindow->showMinimized();
    }
    if (pBtn == m_pCloseBtn)
    {
        // 关闭
        pWindow->close();
    }
}

void AiTitleBar::mousePressEvent(QMouseEvent *event){
    // 发送消息给父窗口，让父窗口处理
    if (ReleaseCapture())
    {
        QWidget *pWindow = this->window();
        if (pWindow->isTopLevel())
        {
            // 发送消息给父窗口，让父窗口移动
            SendMessage(HWND(pWindow->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
    }
}
